/*
 * Copyright (c) 2011 Seoul National University
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Authors: Jung Ho Ahn  ( gajh@snu.ac.kr )
 *
 */

#include <fstream>
#include <iomanip>
#include <sstream>
#include "InputOptions.h"


InputOptions::InputOptions(const string & optionfile, bool is_cin_empty)
 :params()
{
  ifstream fin(optionfile.c_str());

  if (fin.good() == false)
  {
    std::cout << "  -- optionfile can not be opened. default parameters will be used." << std::endl;
  }
  else
  {
    string line, param, value, temp;
    istringstream sline;

    while (getline(fin, line))
    {
      if (line.empty() == true) continue;
      sline.clear();
      sline.str(line);
      sline >> param >> temp >> value;
      if (param.find("#") != string::npos || value.find("#") != string::npos) continue;

      if (param == "fasta_read_filename")
      {
        fasta_filenames.push_back(value);
      }
      else if (param == "fastq_read_filename")
      {
        fastq_filenames.push_back(value);
      }
      else
      {
        params[param] = value;
      }
    }
    while (is_cin_empty == false && getline(cin, line))
    {
      if (line.empty() == true) break;
      sline.clear();
      sline.str(line);
      sline >> param >> temp >> value;
      if (param.find("#") != string::npos || value.find("#") != string::npos) continue;

      if (param == "fasta_read_filename")
      {
        fasta_filenames.push_back(value);
      }
      else if (param == "fastq_read_filename")
      {
        fastq_filenames.push_back(value);
      }
      else
      {
        params[param] = value;
      }
    }
  }
}


InputOptions::~InputOptions()
{
}


bool InputOptions::get_param_bool(const string & str, bool default_value) const
{
  if (params.find(str) == params.end())
  {
    return default_value;
  }
  else
  {
    return (params.find(str)->second == "true");
  }
}


string InputOptions::get_param_str(const string & str) const
{
  if (params.find(str) == params.end())
  {
    return string();
  }
  else
  {
    return params.find(str)->second;
  }
}


uint64_t InputOptions::get_param_uint64(const string & str, uint64_t def) const
{
  if (params.find(str) == params.end())
  {
    return def;
  }
  else
  {
    uint64_t ret = 0;
    const string & value = params.find(str)->second;

    if (value.find("0x", 0) != string::npos || value.find("0X", 0) != string::npos)
    {
      // hex
      for (uint32_t i = 2; i < value.length(); i++)
      {
        ret <<= 4;
        if (value.at(i) >= 'A' && value.at(i) <= 'F')
        {
          ret += (value.at(i) - 'A' + 10);
        }
        else if (value.at(i) >= 'a' && value.at(i) <= 'f')
        {
          ret += (value.at(i) - 'a' + 10);
        }
        else
        {
          ret += (value.at(i) - '0');
        }
      }
    }
    else if (value.find("^", 1) != string::npos)
    {
      // power of N
      istringstream sbase(value.substr(0, value.find("^", 1)));
      istringstream sexp(value.substr(value.find("^", 1) + 1));

      uint64_t base, exp;
      sbase >> base;
      sexp  >> exp;

      ret = 1;
      for (uint32_t i = 0; i < exp; i++)
      {
        ret *= base;
      }
    }
    else
    {
      istringstream sstr(params.find(str)->second);
      sstr >> ret;
    }
    return ret;
  }
}

